package app.kotlin_1.activity

import android.app.DownloadManager
import android.content.Intent
import android.net.Uri
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import app.kotlin_1.R
import app.kotlin_1.adapter.GameDetailTipsAdapter
import app.kotlin_1.base.BaseActivity
import app.kotlin_1.bean.GameBean
import app.kotlin_1.bean.TipsBean
import app.kotlin_1.fragment.CommentFragment
import app.kotlin_1.fragment.IntroduceFragment
import app.kotlin_1.fragment.RecommendFragment
import app.kotlin_1.utils.DownloadAnimation
import app.kotlin_1.utils.inflate
import app.kotlin_1.utils.toast
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import kotlinx.android.synthetic.main.activity_geame_detail.*
import kotlinx.android.synthetic.main.title_back.*
import kotlinx.android.synthetic.main.view_game_detail_header.*
import kotlinx.android.synthetic.main.view_game_detail_title.*

/**
 * Created by 刘智强 on 2017/9/28.
 * 游戏详情
 */
class GameDetailActivity : BaseActivity() {
    private var tipsArray = arrayOf("官方", "无广告", "无病毒", "人工复检")
    private var gameBean: GameBean? = null
    private var tipsList = mutableListOf<TipsBean>()
    private var fragmentList = mutableListOf<Fragment>()
    override fun getlayoutId(): Int = R.layout.activity_geame_detail

    override fun getData() {
        var bundle = intent.extras
        gameBean = bundle?.get("game") as GameBean?
    }

    override fun initData() {
        ivSearch.visibility = View.VISIBLE
        tvTitle.text = gameBean?.name
        sdvGameIcon.setImageURI(gameBean?.icon)
        tvGameName.text = gameBean?.name
        sbStar.starMark = 3.5f
        tvTitleLeft.setBackgroundResource(R.drawable.shape_game_detail_title_left_selected)
        tipsList.add(TipsBean(R.drawable.skull_head, "人工复检 √", "检测专员  华东"))
        tipsList.add(TipsBean(R.drawable.huawei_icon, "华为安全检测 √", ""))
        tipsList.add(TipsBean(R.drawable.safe_icon, "金山安全检测 √", ""))
        tipsList.add(TipsBean(R.drawable.tencent_safe_icon, "腾讯手机管家安全检测 √", ""))
        fragmentList.add(IntroduceFragment())
        fragmentList.add(CommentFragment())
        fragmentList.add(RecommendFragment())


    }

    override fun initView() {

    }

    override fun initListenter() {

        /**
         * 标签的初始化
         */
        tflTips.adapter = object : TagAdapter<String>(tipsArray) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                var tvContent = parent?.inflate(R.layout.item_home_tag) as TextView
                tvContent.text = t
                return tvContent!!
            }
        }
        var flag = false
        ivArrow.setOnClickListener {

            if (flag) {
                flag = false
                ivArrow.setImageResource(R.drawable.arrow_down)
                rvTips.visibility = View.GONE

            } else {
                flag = true
                ivArrow.setImageResource(R.drawable.arrow_up)
                rvTips.visibility = View.VISIBLE
            }


        }
        rvTips.layoutManager = LinearLayoutManager(activity)
//        rvTips.isNestedScrollingEnabled = false
        rvTips.adapter = GameDetailTipsAdapter(activity, tipsList, R.layout.item_game_detail_tips)
        vpGameDetail.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
        vpGameDetail.currentItem = 0

        vpGameDetail.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> selectTitle(0)
                    1 -> selectTitle(1)
                    2 -> selectTitle(2)
                }

            }

        })



        DownloadAnimation.setDownloadAnimationListener(object : DownloadAnimation.DownloadAnimationListener {
            override fun onAnimationEnd() {
                tvDownload.text = "开始下载......"
                tvDownload.postDelayed({ tvDownload.text = "点击安装" }, 3000)
            }
        })

        ivBack.setOnClickListener(this)
        ivSearch.setOnClickListener(this)
        tvDownload.setOnClickListener(this)
        tvTitleLeft.setOnClickListener(this)
        tvTitleCenter.setOnClickListener(this)
        tvTitleRight.setOnClickListener(this)
        ivComments.setOnClickListener(this)
    }

    override fun requestData() {}
    override fun setData(any: Any) {}
    override fun setOnClick(v: View?) {
        when (v?.id) {
            R.id.ivBack -> finish()
            R.id.ivSearch -> startActivity(Intent(activity, SearchActivity::class.java))
            R.id.tvDownload -> {

                DownloadAnimation.AddToCart(ivDownload, tvDownload, activity!!, rlRoot, 3)
            }
            R.id.ivComments -> vpGameDetail.currentItem = 1
            R.id.tvTitleLeft -> {
                vpGameDetail.currentItem = 0
                selectTitle(0)
            }
            R.id.tvTitleCenter -> {
                vpGameDetail.currentItem = 1
                selectTitle(1)
            }
            R.id.tvTitleRight -> {
                vpGameDetail.currentItem = 2
                selectTitle(2)
            }
        }
    }

    fun selectTitle(index: Int) {
        when (index) {
            0 -> {
                activity?.toast(tvTitleLeft.text.toString())
                tvTitleLeft.setBackgroundResource(R.drawable.shape_game_detail_title_left_selected)
                tvTitleCenter.setBackgroundResource(R.drawable.shape_game_detail_title_center)
                tvTitleRight.setBackgroundResource(R.drawable.shape_game_detail_title_right)
            }
            1 -> {
                activity?.toast(tvTitleCenter.text.toString())
                tvTitleLeft.setBackgroundResource(R.drawable.shape_game_detail_title_left)
                tvTitleCenter.setBackgroundResource(R.drawable.shape_game_detail_title_center_selected)
                tvTitleRight.setBackgroundResource(R.drawable.shape_game_detail_title_right)
            }
            2 -> {
                activity?.toast(tvTitleRight.text.toString())
                tvTitleLeft.setBackgroundResource(R.drawable.shape_game_detail_title_left)
                tvTitleCenter.setBackgroundResource(R.drawable.shape_game_detail_title_center)
                tvTitleRight.setBackgroundResource(R.drawable.shape_game_detail_title_right_selected)
            }
        }
    }


}